package com.sunchangpeng.wechat.core.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

@Data
public class WechatError {
    @JSONField(name = "errcode")
    private int errorCode;

    @JSONField(name = "errmsg")
    private String errorMsg;

    private String json;

    public static WechatError fromJson(String json) {
        WechatError error = JSON.parseObject(json, WechatError.class);
        error.setJson(json);
        return error;
    }

    public static String hasEror(String content) {
        WechatError error = WechatError.fromJson(content);
        if (error.getErrorCode() != 0) {
            throw new WechatServerException(error);
        }

        return content;
    }
}
